package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.DoctorWork;
import service.DoctorWorkService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/doctorWork")
public class DoctorWorkController {
    @Autowired
    private DoctorWorkService doctorWorkService;

    @GetMapping("/findAll")
    @ResponseBody
    public List<DoctorWork> findAll() {
        return doctorWorkService.findAll();
    }

    @GetMapping("/findByDocIdAndDay")
    @ResponseBody
    public DoctorWork findByDocIdAndDay(@RequestParam String docId, @RequestParam String day) {
        return doctorWorkService.findByDocIdAndDay(docId, day);
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody DoctorWork doctorWork) {
        Map<String, Object> result = new HashMap<>();
        doctorWork.setCount_left(60);
        int count = doctorWorkService.add(doctorWork);
        result.put("success", count > 0);
        result.put("message", count > 0? "添加成功" : "添加失败");
        return result;
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody DoctorWork doctorWork) {
        Map<String, Object> result = new HashMap<>();
        int count = doctorWorkService.update(doctorWork);
        result.put("success", count > 0);
        result.put("message", count > 0? "更新成功" : "更新失败");
        return result;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam String docId, @RequestParam String day) {
        Map<String, Object> result = new HashMap<>();
        int count = doctorWorkService.delete(docId, day);
        result.put("success", count > 0);
        result.put("message", count > 0? "删除成功" : "删除失败");
        return result;
    }
    @GetMapping("/findByDocId")
    @ResponseBody
    public List<DoctorWork> findByDocId(@RequestParam String docId) {
        return doctorWorkService.findByDocId(docId);
    }
}